﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplLogic.ApplClasses;
using TCPClient.UI.LogicForUserControls;

namespace TCPClient.UI
{
    public partial class WorkerRankingForm : Form
    {
        private eStatType m_StatType;
        private int m_NumOfWorkers = 0;
        private List<ApplWorkerStatInWeek> m_WorkresStat = new List<ApplWorkerStatInWeek>();
        private List<WorkerRanking> m_WorkerRanking = new List<WorkerRanking>();

        private const int k_DefaultMarkedAsLeadingNumber = 2;
        private const int k_DefaultMarkedAsLastNumber = 2;

        public WorkerRankingForm(eStatType statType)
        {
            InitializeComponent();
            m_StatType = statType;
            setTitle();
            collectStatFromServer();
            createTheRanking();

        }

        private void setTitle()
        {
            labelTitle.Text = m_StatType == eStatType.Weekly
                                  ? string.Format("{0}: \n           {1} - {2}", "Weekly Workers Satisfaction Rank"
                                                  ,
                                                  ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek.
                                                      ToShortDateString(),
                                                  ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek.
                                                      ToShortDateString())
                                  : "General Workers Satisfaction Rank";
        }

        private void createTheRanking()
        {
            foreach (var appl in m_WorkresStat)
            {
                m_WorkerRanking.Add( new WorkerRanking
                                         {
                                             FullName = appl.WorkerRef.PersonalDetails.FullName,
                                             SatisfactionRank = (int)appl.GetPrecentOfShiftsThatWantedAndGot() 
                                                + (int)(0.5 * appl.GetPrecentOfShiftsThatGotButNotAsked())  
                                         });
            }
            m_WorkerRanking.Sort();
            m_WorkerRanking.Reverse();
            dataGridView1.DataSource = m_WorkerRanking;
            setColorsAndSizes();

        }

        private void setColorsAndSizes()
        {
            dataGridView1.Columns[0].HeaderText = @"Worker Name";
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.Columns[1].HeaderText = @"Satisfaction Rank (%)";
            dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

        }

        private void collectStatFromServer()
        {
            List<ApplWorker> allWorkers = ScMainUserControlLogicManager.Instance.GetAllWorkers();
            m_NumOfWorkers = allWorkers.Count;
            if(m_StatType == eStatType.Aggregative)
            {
                foreach (var applWorker in allWorkers)
                {
                    m_WorkresStat.Add(ScMainUserControlLogicManager.Instance.GetAllWorkerStatOfWorker(applWorker.PersonalDetails.Id));
                }   
            }
            else
            {
                foreach (var applWorker in allWorkers)
                {
                    m_WorkresStat.Add(ScMainUserControlLogicManager.Instance.GetWorkerStat
                                          (ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek.AddHours(-1),
                                           ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek.AddHours(1),
                                           applWorker.PersonalDetails.Id));
                } 
            }
        }

        private class WorkerRanking : IComparable<WorkerRanking>
        {
            public string FullName { get; set; }
            public int SatisfactionRank { get; set; }

            public int CompareTo(WorkerRanking other)
            {
                if (SatisfactionRank > other.SatisfactionRank)
                {
                    return 1;
                }
                if (SatisfactionRank < other.SatisfactionRank)
                {
                    return -1;
                }
                return 0;
            }
        }




    }
}
